﻿using System.Collections.Generic;
using Android.App;
using Android.Runtime;
using Android.Text;
using Android.Text.Style;
using Android.Widget;
using Java.Lang;

internal sealed class ContentTextWatcher : Java.Lang.Object, ITextWatcher {
    public bool mIsUndoOrRedo;

    private EditHistory mmEditHistory;
    private string mmBeforeChange;
    private string mmAfterChange;

    internal ContentTextWatcher(EditHistory history) {
      mmEditHistory = history;
    }

    public void OnTextChanged(ICharSequence s, int start, int before, int count) {
      if (!mIsUndoOrRedo) {
        mmAfterChange = s.SubSequence(start, start + count);
        mmEditHistory.add(new EditItem(start, mmBeforeChange, mmAfterChange));
      }
    }

    public void BeforeTextChanged(ICharSequence s, int start, int count, int after) {
      if (!mIsUndoOrRedo) {
        mmBeforeChange = s.SubSequence(start, start + count);
      }
    }

    public void AfterTextChanged(IEditable s) {
      //if (mScheduleMoveLeft) {
      //  mScheduleMoveLeft = false;
      //  Selection.MoveLeft(mContentText.EditableText, mContentText.Layout);
      //}
    }
  }

  internal sealed class EditHistory {
    int mmPosition = 0;
    private List<EditItem> mmHistory = new List<EditItem>();

    internal void add(EditItem item) {
      //mmHistory.setSize(mmPosition);
      mmHistory.Add(item);
      mmPosition++;
    }

    internal EditItem getPrevious() {
      if (mmPosition == 0) {
        return null;
      }
      mmPosition--;
      return mmHistory[mmPosition];
    }

    internal EditItem getNext() {
      if (mmPosition == mmHistory.Count) {
        return null;
      }
      EditItem item = mmHistory[mmPosition];
      mmPosition++;
      return item;
    }
  }

  internal sealed class EditItem {
    internal int mmIndex;
    internal string mmBefore;
    internal string mmAfter;

    public EditItem(int start, string before, string after) {
      mmIndex = start;
      mmBefore = before;
      mmAfter = after;
    }
  }

